import { SET_SUB, SET_BTN, SET_PERMISSIONROUTER, DEL_PERMISSIONROUTER, SET_ROUTER } from '../mutation-types';
import { toRouter } from '@/utils/listToTree';

import router from '../../router/index'

// import constantRoutes from '../../router/modules/constantRoutes' // 初始化路由
import noneRouter from '../../router/modules/noneRouter' // 404
import log from '../../router/modules/log'
import error from '../../router/modules/error'
import menu from '../../router/modules/menu'
import role from '../../router/modules/role'
import user from '../../router/modules/user'
import center from '../../router/modules/center'
import setting from '../../router/modules/setting'
import home from '../../router/modules/home'
import notice from '../../router/modules/notice'
import Settled from '../../router/modules/Settled'
import business from '../../router/modules/business'
import enterCheck from "../../router/modules/enterCheck"
import SetCategory from "../../router/modules/SetCategory"
import brand from "../../router/modules/brand"

let routerChild = [{
  path: '/admin',
  component: resolve => require(['../../views/index'], resolve),
  children: [
    {
      path: '/setting',
      name: '账号配置',
      component: resolve => require(['../../views/setting/BaseSetting'], resolve),
      meta: {
        title: '基础配置',
      }
    },
    {
      path: '/home',
      name: '首页',
      component: resolve => require(['../../views/home'], resolve)
    },
    // {
    //   path: 'menu/configMenu',
    //   name: '系统管理/编辑菜单',
    //   component: resolve =>
    //     require (['../../views/menu/configmenu'], resolve)
    // },
    // {
    //   path: 'menu/addMenu',
    //   name: '系统管理/新增菜单',
    //   component: resolve =>
    //     require (['../../views/menu/addMenu'], resolve)
    // }
  ]
}]

const state = {
  routeName: [], // 左侧title名字
  sub: [], // 左侧menu列表
  btns: [], // 权限按钮
  permissionRouter: [] // 动态路由
};

const mutations = {
  [SET_ROUTER]: (state, routeName) => {
    state.routeName = []
    routeName.forEach((item) => {
      state.routeName.push(item)
    })
  },
  [SET_SUB] (state, sub) {
    state.sub = sub || []
  },
  [SET_BTN] (state, btns) {
    state.btns = btns || []
  },
  [SET_PERMISSIONROUTER] (state, permissionRouter) {
    let routerCount = toRouter(permissionRouter)
    state.permissionRouter = permissionRouter || [];
    // let arrRouter = [...brand,...SetCategory,...enterCheck,...Settled,...business,...notice, ...home, ...log, ...setting, ...center, ...error, ...menu, ...role, ...user]
      // let arr = []
      // permissionRouter.forEach((item) => {
      //   arrRouter.forEach((_item) => {
      //     if ('/' + item === _item.path) {
      //       arr.push(_item)
      //     }
      //   })
      // })
      routerChild[0].children = [...routerChild[0].children, ...routerCount]
      routerChild = [...routerChild, ...noneRouter]
      console.log(routerChild, 'routerChild')
      router.addRoutes(routerChild)
  },
  [DEL_PERMISSIONROUTER] (state) {
    state.permissionRouter = [];
  }
};

export default {
  state,
  mutations,
};
